<script lang="ts" setup>
import { computed, h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { formatDate } from '@vben/utils';

import { DescriptionChange } from '#/components/description-change';
import DictTag from '#/components/dict-tag/dict-tag.vue';
import { DictBadge } from '#/components/dict-badge';

interface DescriptionItemSchema {
  label: string;
  field: string;
  span?: number;
  render?: (val: any) => any;
}

const props = defineProps<{ data?: any }>();
const formData = computed(() => props.data || {});

function formatDateSafe(
  date: null | string | undefined,
  format = 'YYYY-MM-DD',
) {
  if (!date) return '';
  const d = new Date(date);
  return Number.isNaN(d.getTime()) ? '' : formatDate(d, format);
}

/** 家庭成员从业情况 Schema */
const familyEmploymentSchema = computed<DescriptionItemSchema[]>(() => [
  { label: '姓名', field: 'familyName', span: 12 },
  {
    label: '与本人关系',
    field: 'relation',
    render: (val) => h(DictTag, { type: DICT_TYPE.PER_RELATION, value: val }),
    span: 12,
  },
  {
    label: '证件类型',
    field: 'cardType',
    render: (val) =>
      h(DictTag, { type: DICT_TYPE.SYSTEM_CARD_TYPE, value: val }),
    span: 12,
  },
  { label: '证件号码', field: 'familyIdcard', span: 12 },
  { label: '单位名称', field: 'employerName', span: 12 },
  {
    label: '单位性质',
    field: 'employerType',
    render: (val) =>
      h(DictTag, { type: DICT_TYPE.PER_EMPLOYER_TYPE, value: val }),
    span: 12,
  },
  { label: '职务', field: 'positionName', span: 12 },
  {
    label: '是否担任高级职务',
    field: 'isSeniorPosition',
    render: (val) =>
      h(DictBadge, {
        value: val,
      }),
    span: 12,
  },
  {
    label: '是否共同生活',
    field: 'isLiveTogether',
    render: (val) =>
      h(DictBadge, {
        value: val,
      }),
    span: 12,
  },
  {
    label: '是否在职',
    field: 'isEmployed',
    render: (val) =>
      h(DictBadge, {
        value: val,
      }),
    span: 12,
  },
  { label: '备注', field: 'remarks', span: 24 },
  {
    label: '状态',
    field: 'status',
    render: (val) => h(DictTag, { type: DICT_TYPE.COMMON_STATUS, value: val }),
    span: 12,
  },

  {
    label: '更新时间',
    field: 'updateTime',
    render: formatDateSafe,
    span: 12,
  },
]);
</script>

<template>
  <DescriptionChange
    v-if="formData"
    :info="formData"
    :schema="familyEmploymentSchema"
    :change-list="formData.changeContent?.fieldChanges || []"
    :highlight-mode="formData.highlightMode"
    :change-type="formData.changeType"
    :label-width="120"
  />
</template>
